﻿using System;
using System.IO;
using System.Xml.Linq;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;
using Newtonsoft.Json;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x02000098 RID: 152
    public static class YTPayProxy {
        // Token: 0x0600057B RID: 1403 RVA: 0x000053BD File Offset: 0x000035BD
        public static void AddYTPayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("POST/soap/ytpay.asmx", new HttpContextActionDelegate(Process));
        }

        // Token: 0x0600057C RID: 1404 RVA: 0x00011724 File Offset: 0x0000F924
        private static void Process(HttpContext ctx) {
            YTPayResult ytpayResult = new YTPayResult();
            try {
                XDocument xdocument = XDocument.Parse(new StreamReader(ctx.Request).ReadToEnd());
                YTPayRequest ytpayRequest = xdocument.Root.Value.JsonDecode<YTPayRequest>();
                string text;
                _ = ctx.Request.Header.TryGetValue("SOAPAction", out text);
                if (!text.Equals("\"http://api.yt-pos.cn/Exec\"", StringComparison.OrdinalIgnoreCase)) {
                    throw new Exception("不支持该业务！");
                }
                HongshPayTrade hongshPayTrade = ctx.Execute(new TradeAuthPaymentRequest {
                    Amount = (int)Math.Ceiling(HongshExtends.ConvertTo<decimal>(ytpayRequest.SheetAmt) * 100m),
                    Authcode = ytpayRequest.AuthCode,
                    Subject = ytpayRequest.Title
                });
                DateTime t = DateTime.Now.AddSeconds(30.0);
                ytpayResult.ChannelOrderNum = hongshPayTrade.TradeId;
                while (hongshPayTrade.State == TradeState.未知 && DateTime.Now <= t) {
                    hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                        TradeId = ytpayResult.ChannelOrderNum
                    });
                }
                if (hongshPayTrade.State != TradeState.成功) {
                    if (hongshPayTrade.State == TradeState.失败 || hongshPayTrade.State == TradeState.关闭) {
                        throw new Exception(string.IsNullOrEmpty(hongshPayTrade.StateDescript) ? "交易失败或超时，请撤销交易！" : hongshPayTrade.StateDescript);
                    }
                    ctx.KeepAlive = false;
                    return;
                }
            }
            catch (Exception ex) {
                ytpayResult.MsgCode = -1;
                ytpayResult.ErrorDetail = ex.Message;
            }
            ctx.KeepAlive = false;
            ctx.Result = new HttpTextResult("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><ExecResponse xmlns=\"http://api.yt-pos.cn\"><ExecResult>" + ytpayResult.JsonEncode(Formatting.None) + "</ExecResult></ExecResponse></soap:Body></soap:Envelope>", "text/xml");
        }

        // Token: 0x02000099 RID: 153
        private class YTPayRequest {
            // Token: 0x17000265 RID: 613
            // (get) Token: 0x0600057D RID: 1405 RVA: 0x000053DB File Offset: 0x000035DB
            // (set) Token: 0x0600057E RID: 1406 RVA: 0x000053E3 File Offset: 0x000035E3
            public string PayId { get; set; }

            // Token: 0x17000266 RID: 614
            // (get) Token: 0x0600057F RID: 1407 RVA: 0x000053EC File Offset: 0x000035EC
            // (set) Token: 0x06000580 RID: 1408 RVA: 0x000053F4 File Offset: 0x000035F4
            public string SheetAmt { get; set; }

            // Token: 0x17000267 RID: 615
            // (get) Token: 0x06000581 RID: 1409 RVA: 0x000053FD File Offset: 0x000035FD
            // (set) Token: 0x06000582 RID: 1410 RVA: 0x00005405 File Offset: 0x00003605
            public string RtnAmt { get; set; }

            // Token: 0x17000268 RID: 616
            // (get) Token: 0x06000583 RID: 1411 RVA: 0x0000540E File Offset: 0x0000360E
            // (set) Token: 0x06000584 RID: 1412 RVA: 0x00005416 File Offset: 0x00003616
            public string AuthCode { get; set; }

            // Token: 0x17000269 RID: 617
            // (get) Token: 0x06000585 RID: 1413 RVA: 0x0000541F File Offset: 0x0000361F
            // (set) Token: 0x06000586 RID: 1414 RVA: 0x00005427 File Offset: 0x00003627
            public string TradeNo { get; set; }

            // Token: 0x1700026A RID: 618
            // (get) Token: 0x06000587 RID: 1415 RVA: 0x00005430 File Offset: 0x00003630
            // (set) Token: 0x06000588 RID: 1416 RVA: 0x00005438 File Offset: 0x00003638
            public string Title { get; set; }

            // Token: 0x06000589 RID: 1417 RVA: 0x0000286D File Offset: 0x00000A6D
            public YTPayRequest() {

            }
        }

        // Token: 0x0200009A RID: 154
        private class YTPayResult {
            // Token: 0x1700026B RID: 619
            // (get) Token: 0x0600058A RID: 1418 RVA: 0x00005441 File Offset: 0x00003641
            // (set) Token: 0x0600058B RID: 1419 RVA: 0x00005449 File Offset: 0x00003649
            public string ChannelOrderNum { get; set; }

            // Token: 0x1700026C RID: 620
            // (get) Token: 0x0600058C RID: 1420 RVA: 0x00005452 File Offset: 0x00003652
            // (set) Token: 0x0600058D RID: 1421 RVA: 0x0000545A File Offset: 0x0000365A
            public string ErrorDetail { get; set; }

            // Token: 0x1700026D RID: 621
            // (get) Token: 0x0600058E RID: 1422 RVA: 0x00005463 File Offset: 0x00003663
            // (set) Token: 0x0600058F RID: 1423 RVA: 0x0000546B File Offset: 0x0000366B
            public int MsgCode { get; set; }

            // Token: 0x1700026E RID: 622
            // (get) Token: 0x06000590 RID: 1424 RVA: 0x00005474 File Offset: 0x00003674
            // (set) Token: 0x06000591 RID: 1425 RVA: 0x0000547C File Offset: 0x0000367C
            public string Msg { get; set; }

            // Token: 0x1700026F RID: 623
            // (get) Token: 0x06000592 RID: 1426 RVA: 0x00005485 File Offset: 0x00003685
            // (set) Token: 0x06000593 RID: 1427 RVA: 0x0000548D File Offset: 0x0000368D
            public string Data { get; set; }

            // Token: 0x06000594 RID: 1428 RVA: 0x0000286D File Offset: 0x00000A6D
            public YTPayResult() {

            }
        }
    }
}
